<?php

namespace Com\AlipayBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Com\AlipayBundle\Lib\AlipayNotify;

class AlipayController extends Controller {

    public function notifyAction() {
        /* @var $alipayNotify \Com\AlipayBundle\Lib\AlipayNotify */
        $alipayNotify = $this->get('com_alipay.notify');
        $result = $alipayNotify->verifyNotify();

        if ($result) {
            $post = $this->getRequest()->request;

            // 订单号
            $outTradeNo = $post->get('out_trade_no');
            // 支付宝交易号
            $tradeNo = $post->get('trade_no');
            // 交易状态
            $tradeStatus = $post->get('trade_status');

            $em = $this->getDoctrine()->getManager();
            $entity = new \Wilead\IArtistBundle\Entity\AlipayHistory();
            $entity->setOutTradeNo($outTradeNo);
            $entity->setTradeNo($tradeNo);
            $entity->setTradeStatus($tradeStatus);
            $entity->setInfo($_POST);

            $em->persist($entity);
            $em->flush();

            // 交易完成
            if ($tradeStatus == AlipayNotify::FINISHED) {
                
            }
            echo "success";
        } else {
            echo "fail";
        }
        exit();
    }

}
